using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using Utilities;

namespace Services.Menu
{

    public class LoadingScreen : DrawableGameComponent
    {
        public event Action OnLoadBegin;
        public event Action OnLoadEnd;

        SpriteBatch sprite_batch;
        ContentManager Content { get { return Game.Content; } }
        Texture2D bg;
        SpriteFont font;
        Matrix screen_transform;
        Texture2D img, glowing_point;

        public bool start_fadeoff_transition = false;
        bool first_update = true, first_draw = true;
        GraphicsDeviceManager graphics;
        public LoadingScreen(Game game, GraphicsDeviceManager graphics)
            : base(game)
        {
            this.UpdateOrder = (int)ComponentOrders.LOADING;
            this.DrawOrder = (int)ComponentOrders.LOADING;
            sprite_batch = (SpriteBatch)game.Services.GetService(typeof(SpriteBatch));

            //screen_transform = Matrix.CreateScale(game.GraphicsDevice.Viewport.Width / 480.0f, game.GraphicsDevice.Viewport.Height / 800.0f, 1.0f);
            screen_transform = Game.ScreenTransformMatrix(graphics);
            this.graphics = graphics;
        }

        public override void Initialize()
        {
            this.Enabled = true;

            base.Initialize();
        }

        Vector2 img_pos;
        protected override void LoadContent()
        {
            bg = Content.Load<Texture2D>("UI/WhitePixel");
            img = Content.Load<Texture2D>("Menu/loading_bar");
            glowing_point = Content.Load<Texture2D>("Menu/glowing_point");
            font = Content.Load<SpriteFont>("Fonts/OCRAExtended30");

            

            base.LoadContent();
        }

        float fade_off_timer = 3.0f;
        public override void Update(GameTime gameTime)
        {
            var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            interp_val = (1 - (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds)) / 2.0f;

            if (start_fadeoff_transition) fade_off_timer -= dt;

            if (first_update && !first_draw)
            {
                OnLoadBegin();
                first_update = false;
            }

            if (fade_off_timer <= -2.0f)
            {
                Game.Components.Remove(this);

                for (int i = 0; i < Game.Components.Count; i++)
                    ((GameComponent)Game.Components[i]).Enabled = true;
            }

            base.Update(gameTime);
        }

        protected override void OnEnabledChanged(object sender, EventArgs args)
        {
            base.OnEnabledChanged(sender, args);
        }

        string load = "Loading";
        Vector2 initial_pos_portrait = new Vector2(28, 342), final_pos_portrait = new Vector2(412,342);
        Vector2 initial_pos_landscape = new Vector2(188, 182), final_pos_landscape = new Vector2(572, 182);
        float interp_val;
        public override void Draw(GameTime gameTime)
        {

            var alpha = Math.Min(fade_off_timer,1.0f);

            sprite_batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screen_transform);
            if (graphics.SupportedOrientations == DisplayOrientation.Portrait || graphics.SupportedOrientations == DisplayOrientation.Default)
            {
                img_pos = new Vector2(240 - img.Width / 2.0f, 400 - img.Height / 2.0f);
                sprite_batch.Draw(bg, new Rectangle(0, 0, 480, 800), Color.Black * alpha);
                sprite_batch.Draw(img, img_pos, Color.White * alpha);
                sprite_batch.Draw(glowing_point, Vector2.Lerp(initial_pos_portrait, final_pos_portrait, interp_val), Color.White * alpha);
                sprite_batch.Draw(glowing_point, Vector2.Lerp(final_pos_portrait, initial_pos_portrait, interp_val), Color.White * alpha);
                sprite_batch.DrawString(font, load,
                    new Vector2(240.0f - font.MeasureString(load).X / 2.0f, img_pos.Y - 50), Color.White * alpha);
            }
            else
            {
                img_pos = new Vector2(400 - img.Width / 2.0f, 240 - img.Height / 2.0f);
                //Console.WriteLine(Mouse.GetState().X + " " + Mouse.GetState().Y);
                sprite_batch.Draw(bg, new Rectangle(0, 0, 800, 480), Color.Black * alpha);
                sprite_batch.Draw(img, img_pos, Color.White * alpha);
                sprite_batch.Draw(glowing_point, Vector2.Lerp(initial_pos_landscape, final_pos_landscape, interp_val), Color.White * alpha);
                sprite_batch.Draw(glowing_point, Vector2.Lerp(final_pos_landscape, initial_pos_landscape, interp_val), Color.White * alpha);
                sprite_batch.DrawString(font, load,
                    new Vector2(400f - font.MeasureString(load).X / 2.0f, img_pos.Y - 50), Color.White * alpha);

            }
            sprite_batch.End();

            if (first_draw) first_draw = false;

            base.Draw(gameTime);
        }

        public void LoadProccesComplete()
        {
            for (int i = 0; i < Game.Components.Count; i++)
            {
                if (Game.Components[i] as LoadingScreen != null)
                    ((LoadingScreen)Game.Components[i]).OnLoadEnd();                
            }
            start_fadeoff_transition = true;
        }
    }
}
